//
//  UserModel.m
//  Lottery
//
//  Created by 桑赐相 on 2018/7/15.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "UserModel.h"

@implementation UserModel

MJCodingImplementation

+(NSArray *)getUserTableView{
    
    XLUserModel *user = [XLUserModel getUserInfo];
    
    NSMutableArray *group = [NSMutableArray array];
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    dict[@"name"] = @"电话";
    dict[@"value"] = [user.phone numberSuitScanf];
    dict[@"key"] = @"phone";
    [array addObject:dict];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"姓名";
    dict[@"value"] = user.name;
    dict[@"key"] = @"name";
    [array addObject:dict];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"性别";
    dict[@"value"] = user.gender ? user.gender : @"未填写";
    dict[@"key"] = @"gender";
    [array addObject:dict];
    
//    dict = [NSMutableDictionary dictionary];
//    dict[@"name"] = @"年龄";
//    dict[@"value"] = user.age;
//    dict[@"key"] = @"age";
//    [array addObject:dict];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"身高";
    dict[@"value"] = user.height;
    dict[@"key"] = @"height";

    [array addObject:dict];
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"身份";
    dict[@"value"] = user.identity;
    dict[@"key"] = @"identity";

    [array addObject:dict];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"常住地址";
    dict[@"value"] = user.address;
    dict[@"key"] = @"address";
    [array addObject:dict];
    [group addObject:array];
    
    array = [NSMutableArray array];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"毕业学校";
    dict[@"value"] = user.university;
    dict[@"key"] = @"university";
    [array addObject:dict];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"最高学历";
    dict[@"value"] = user.education;
    dict[@"key"] = @"education";
    [array addObject:dict];
    
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"入学年份";
    dict[@"value"] = user.enrollmentYear;
    dict[@"key"] = @"enrollmentYear";
    [array addObject:dict];
    [group addObject:array];
    
    array = [NSMutableArray array];
    dict = [NSMutableDictionary dictionary];
    dict[@"name"] = @"自我描述";
    dict[@"value"] = user.about;
    dict[@"key"] = @"about";
    [array addObject:dict];
    [group addObject:array];
    
    return group;
}


+(void)saveUserInfo:(UserModel *)user{
    [NSKeyedArchiver archiveRootObject:user toFile:UserPath];
}

+(void)removeUserInfo{
    [[NSFileManager defaultManager] removeItemAtPath:UserPath error:nil];
}

+(UserModel *)getUserInfo{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:UserPath];;
}


+(void)getServiceUserInfo:(void (^)(UserModel *))callback{
    UserModel *user = [self getUserInfo];
    if (!user) return;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [HTTPTools GET:getUserInfoUrl parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        if (!error) {
            if (!msg) {
                UserModel *user = [UserModel mj_objectWithKeyValues:result];
                [UserModel saveUserInfo:user];
                if (callback) {
                    callback(user);
                }
            }else{
                [UserModel removeUserInfo];
                [iToast showCenter:@"账号已过期请重新登录"];
                
            }
        }
    }];
}


@end

